<?php
/**
 * @ClassName: Index
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2024-09-08 10:17
 * @Version: V1.0.0
 */

namespace app\api\controller;

use app\common\model;
use think\Controller;

class Index extends Controller
{
    /**
     * 网站配置
     * @return \think\response\Json
     */
    function getCopyRight()
    {
        $data = sys_config('web_config');

        return json([
            'code' => 1,
            'data' => $data,
        ]);
    }

    /**
     * 系统主题
     * @return \think\response\Json
     */
    function getTheme()
    {
        $data = model\Theme::where(['status' => 1])->find();
        if ($data) {
            $data['bg_pic'] = replace_url($data['bg_pic']);
        }

        return json([
            'code' => 1,
            'data' => $data,
        ]);
    }

    /**
     * 引导图
     * @return \think\response\Json
     */
    function getGuide()
    {
        $swiper = model\CmsBanner::where(['position_id' => 2, 'is_show' => 1])
//            ->cache('appGuide', mt_rand(7000, 8000))
            ->order('sort')
            ->field('id,title,link,image')
            ->select();

        foreach ($swiper as $key => $value) {
            $swiper[$key]['image'] = replace_url($value['image']);
        }

        return json([
            'code' => 1,
            'data' => $swiper,
        ]);
    }

    /**
     * 首页轮播图
     * @return \think\response\Json
     */
    function getBanner()
    {
        $swiper = model\CmsBanner::where(['position_id' => 1, 'is_show' => 1])
            ->order('sort')
            ->field('id,title,link,image')
            ->select();

        foreach ($swiper as $key => $value) {
            $swiper[$key]['image'] = replace_url($value['image']);
        }

        return json([
            'code' => 1,
            'data' => $swiper,
        ]);
    }

    /**
     * 首页金刚区菜单
     * @return \think\response\Json
     */
    function getMenu()
    {
        $menu = model\CmsBanner::where(['position_id' => 3, 'is_show' => 1])
//            ->cache('index_menu', mt_rand(7000, 8000))
            ->order('sort')
            ->field('id,title,link,image')
            ->select();

        foreach ($menu as $key => $value) {
            $menu[$key]['image'] = replace_url($value['image']);
        }

        return json([
            'code' => 1,
            'data' => $menu,
        ]);
    }

    /**
     * APP版本更新
     * @param $version
     * @param $type
     * @return \think\response\Json
     */
    function getAppVersion($version = '1.0.0', $type = 'android')
    {
        $systemType = $type;
        $model = new model\AppVersion();

        $arr = explode('.', strval($version));

        $version_code = '';
        foreach ($arr as $i) {
            $version_code .= $i;
        }
        if (empty($version_code)) {
            $version_code = '100';
        }

        $fullUpdate = $model->where(['type' => $systemType, 'full_update' => 1])->order('version_code desc')->find();
        // 如果更新的版本小于最后一次整包更新的 就更新整包
        if ($fullUpdate && $version_code <= $fullUpdate['version_code']) {
            $info = $fullUpdate;
        } else {
            $info = $model->where(['type' => $systemType])
                ->order('create_time', 'desc')
                ->find();
        }

        if ($info && $version_code < $info['version_code']) {
            return json([
                'code' => 1,
                'data' => [
                    'isUpdate' => true,
                    'download_url' => $info['download_url'],
                    'update_text' => $info['update_text'],
                    'full_update' => $info['full_update'],
                    'new_version' => $info['new_version'],
                    'force' => $info['force'],
                ],
            ]);
        }
        return json([
            'code' => 0,
            'data' => [
                'isUpdate' => false,
            ],
        ]);
    }

    /**
     * 支付方式（官方支付、星驿付支付、汇付支付）
     * @return \think\response\Json
     */
    function getPayType()
    {
        //支付方式
        $pay_config = sys_config('payment');

        return json([
            'code' => 1,
            'data' => [
                'pay_type' => $pay_config['pay_type'],
            ],
        ]);
    }

    /**
     * 用户配置
     * @return \think\response\Json
     */
    function getUserConf()
    {
        $data = sys_config('user_config');

        return json([
            'code' => 1,
            'data' => $data,
        ]);
    }

}